package link

import (
	"github.com/gin-gonic/gin"
	"github.com/vueadmin/app/admin/internal/dto"
	"github.com/vueadmin/app/admin/internal/logic"
	"github.com/vueadmin/app/admin/internal/svc"
	"github.com/vueadmin/utils"
	"github.com/vueadmin/utils/common/request"
	"github.com/vueadmin/utils/common/result"
	"github.com/vueadmin/utils/response"
)

/**
 * @description(数据列表)
 * @buildcode(true)
*/
func GetPageList(c *gin.Context, svcCtx *svc.ServiceContext) {
	req := dto.LinkPageReq{
		PageReq:request.PageReq{Page: 1,Limit: 20},
	}
	if err := utils.BindAndValid(c, &req); err != nil {
		response.Fail(c, err.Error())
		return
	}
	l := logic.NewLinkLogic(c,svcCtx)
	list, count, err := l.GetPageList(&req)
	if err != nil {
		response.Err(c, err)
		return
	}
	var sqldata map[string]interface{}
	if req.Page == 1 {
		sqldata, err = l.GetFieldList(c)
		if err != nil {
			response.Err(c, err)
			return
		}
	}
	response.Success(c, "返回成功", result.M{
		"data":  list,
		"total": count,
		"sql_field_data":sqldata,
	})
}

/**
 * @description(修改排序开关)
 * @buildcode(true)
*/
func UpdateExt(c *gin.Context, svcCtx *svc.ServiceContext) {
	var req map[string]interface{}
	if err := utils.BindAndValid(c, &req); err != nil {
		response.Fail(c, err.Error())
		return
	}
	l := logic.NewLinkLogic(c,svcCtx)
	if err := l.UpdateExt(req);err != nil{
		response.Err(c, err)
		return
	}
	response.Success(c, "修改成功", nil)
}

/**
 * @description(添加)
 * @buildcode(true)
*/
func Add(c *gin.Context, svcCtx *svc.ServiceContext) {
	req := dto.LinkAddReq{}
	if err := utils.BindAndValid(c, &req); err != nil {
		response.Fail(c, err.Error())
		return
	}
	l := logic.NewLinkLogic(c,svcCtx)
	ret, err := l.Add(&req)
	if err != nil {
		response.Err(c, err)
		return
	}
	response.Success(c, "返回成功", ret)
}

/**
 * @description(修改)
 * @buildcode(true)
*/
func Update(c *gin.Context, svcCtx *svc.ServiceContext) {
	req := dto.LinkUpdateReq{}
	if err := utils.BindAndValid(c, &req); err != nil {
		response.Fail(c, err.Error())
		return
	}
	l := logic.NewLinkLogic(c,svcCtx)
	if err := l.Update(&req); err != nil{
		response.Err(c, err)
		return
	}
	response.Success(c, "修改成功", nil)
}

/**
 * @description(获取修改详情)
 * @buildcode(true)
*/
func GetUpdateInfo(c *gin.Context, svcCtx *svc.ServiceContext) {
	req := dto.LinkGetUpdateInfoReq{}
	if err := utils.BindAndValid(c, &req); err != nil {
		response.Fail(c, err.Error())
		return
		}
	l := logic.NewLinkLogic(c,svcCtx)
	info,err := l.GetUpdateInfo(&req)
	if err != nil {
		response.Err(c, err)
		return
	}
	response.Success(c, "返回成功", info)
}

/**
 * @description(删除)
 * @buildcode(true)
*/
func Delete(c *gin.Context, svcCtx *svc.ServiceContext) {
	req := dto.LinkDeleteReq{}
	if err := utils.BindAndValid(c, &req); err != nil {
		response.Fail(c, err.Error())
		return
	}
	l := logic.NewLinkLogic(c,svcCtx)
	if err := l.Delete(&req); err != nil{
		response.Err(c, err)
		return
	}
	response.Success(c, "删除成功", nil)
}

/**
 * @description(查看详情)
 * @buildcode(true)
*/
func Detail(c *gin.Context, svcCtx *svc.ServiceContext) {
	req := dto.LinkDetailReq{}
	if err := utils.BindAndValid(c, &req); err != nil {
		response.Fail(c, err.Error())
		return
	}
	l := logic.NewLinkLogic(c,svcCtx)
	info,err := l.Detail(&req)
	if err != nil {
		response.Err(c, err)
		return
	}
	response.Success(c, "返回成功", info)
}

/**
 * @description(查询sql下拉列表)
 * @buildcode(true)
*/
func GetFieldList(c *gin.Context, svcCtx *svc.ServiceContext) {
	l := logic.NewLinkLogic(c,svcCtx)
	data, err := l.GetFieldList(c)
	if err != nil {
		response.Fail(c, err.Error())
		return
	}
	response.Success(c, "返回成功", data)
}

